<?php
if(!defined("BO_ROOT")){
    exit("NO BO");
}
/**
 * 字符操作的静态类,所有通用的静态函数都在此
 *
 */
class str {

    /**
     * 移除HTML标签(标题和留言用)
     * @param string
     * @return string
     */
    public static function moveHtml($str, $length='') {

        if (is_array($str)) {
            foreach ($str as $key => $value) {
                $str[$key] = self::moveHtml($value, $length);
            }
        } else {
            $tag = "";
            $str = strip_tags($str, $tag);
            $str = trim($str);
            $str = str_ireplace('<br />', '', $str);
            $str = str_replace(array('"', "'"), array("&quot;", "&#039;"), $str);
            //$str = trim($str,'　');//添加这个有乱码
            if (!empty($length)) {
                $len = $length + 1;
                if (isset($str[$len]))
                    $str = self::substrSelf($str, 0, $length);
            }
        }
        //$str = str_replace('　','',$str);
        //echo $str;exit;
        return $str;
    }

    /**
     * 移除HTML标签(去除有害代码)
     * @param string
     * @return string
     */
    public static function moveHtmlDetail($str, $moveimg=false, $movetable=true, $movediv=false, $movespan=false, $moveul=false, $moveh17=false, $movelink=true) {
        if (is_array($str)) {
            foreach ($str as $key => $value) {
                $str[$key] = self::moveHtmlDetail($value);
            }
        } else {
            $tag = "<b>,<i>,<font>,<strong>,<br>,<p>,<strike>,<embed>,<hr>,<blockquote>";
            if (!$movediv)
                $tag .= "<div>";
            if (!$moveh17)
                $tag .= "<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<h7>";
            if (!$moveimg)
                $tag .= "<img>";
            if (!$movespan)
                $tag .= "<span>";
            if (!$movetable)
                $tag .= "<table>,<tbody>,<tr>,<th>,<td>";
            if (!$moveul)
                $tag .= "<ul>,<ol>,<li>";
            if (!$movelink)
                $tag .= "<a>";
            $tag .= strtoupper($tag);
            $str = strip_tags($str, $tag);
            $str = trim($str);
        }
        return $str;
    }

    /**
     * 截取字符串
     */
    public static function substrSelf($str, $start=0, $len=80, $flag = '') {
        $str = self::moveHtml($str); //此处没用
        $strlen = mb_strlen($str);
        for ($i = 0; $i < $len; $i++) {
            $temp_str = substr($str, 0, 1);
            if (ord($temp_str) > 127) {
                $i++;
                if ($i < $len) {
                    $new_str[] = substr($str, 0, 3);
                    $str = substr($str, 3);
                }
            } else {
                $new_str[] = substr($str, 0, 1);
                $str = substr($str, 1);
            }
        }
        if ($strlen > $len)
            return join($new_str) . $flag;
        else
            return join($new_str);
    }

    /**
     * 截取字符,但是保留html代码
     * @param unknown_type $str_cut
     * @param unknown_type $start
     * @param unknown_type $length
     * @param unknown_type $flag
     */
    public static function substrHavehtml($str_cut, $start=0, $length=80, $flag = '') {
        if (strlen($str_cut) > $length) {
            for ($i = 0; $i < $length; $i++)
                if (ord($str_cut[$i]) > 128)
                    $i++;
            $str_cut = substr($str_cut, $start, $i) . $flag;
        }
        return $str_cut;
    }

    /**
     * 将二维数组中某个字段截取某个长度
     * 将原数据保留在 $keyname_old中
     */
    public static function substrByArray($arraystr, $keyname, $length=80, $flag='') {
        if (is_array($arraystr)) {
            foreach ($arraystr as $key => $row) {
                if (is_array($row)) {
                    foreach ($row as $key2 => $value) {
                        if ($key2 == $keyname) {
                            $arraystr[$key][$keyname] = self::substrSelf($value, 0, $length, $flag);
                            $arraystr[$key][$keyname . '_old'] = $value;
                        }
                    }
                } else {
                    if ($key == $keyname) {
                        $arraystr[$keyname] = self::substrSelf($row, 0, $length, $flag);
                        $arraystr[$keyname . '_old'] = $value;
                    }
                }
            }
        }
        return $arraystr;
    }

    /**
     * 产生js的alert
     */
    public static function alert($str, $charset='utf-8') {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        echo '<meta http-equiv="cache-control" content="no-cache">';
        echo '<script lanage="javascript">alert("' . $str . '")</script>';
        exit;
    }

    /**
     * 输出js警告，不exit
     * @param type $str
     * @param type $charset 
     */
    public static function alertNoExit($str, $charset='utf-8') {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        echo '<meta http-equiv="cache-control" content="no-cache">';
        echo '<script lanage="javascript">alert("' . $str . '")</script>';
    }

    /**
     * 产生js并关闭
     *
     * @param unknown_type $str
     * @param unknown_type $charset
     */
    public static function alertClose($str, $charset='utf-8') {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        echo '<script lanage="javascript">alert("' . $str . '");</script>';
        echo '<script lanage="javascript">window.close();</script>';
        exit;
    }

    /**
     * 产生js的警告，并后退
     */
    public static function back($str, $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        echo '<meta http-equiv="pragma" content="no-cache">';
        echo '<meta http-equiv="cache-control" content="no-cache">';
        echo '<meta http-equiv="expires" content="0">';
        self::playsound($sound);
        echo '<script lanage="javascript">alert("' . $str . '");' . 'history.back();</script>';
        exit;
    }

    /**
     * 产生js警告，并转向到某个地址
     */
    public static function jump($url, $str, $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        self::playsound($sound);
        echo '<script lanage="javascript">alert("' . $str . '");' . 'window.location="' . $url . '"</script>';
        exit;
    }

    public static function jumpEchoSound($url, $str, $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        echo '<meta http-equiv="refresh" content="3;url=' . $url . '">';
        self::playsound($sound);
        if (!empty($str))
            echo $str . '<br />';
        echo '<a href="' . $url . '">如不能跳转,请点击此处</a>';
        exit;
    }

    /**
     * 转向到某地址
     */
    public static function jumpUrl($url, $charset='utf-8') {
        header("Content-type:text/html;charset=$charset");
        echo '<script lanage="javascript">window.location="' . $url . '";</script>';
        exit;
    }

    /**
     * 输出跳转到下一页的提示
     *
     * @param unknown_type $url
     * @param unknown_type $str
     * @param unknown_type $time
     * @param unknown_type $charset
     */
    public static function jumpEcho($url, $str='', $time = 3, $charset='utf-8') {
        header("Content-type:text/html;charset=$charset");
        echo '<html><head>';
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        echo '<title>自动跳转中</title>';
        echo '<meta http-equiv="refresh" content="' . $time . ';url=' . $url . '">';
        echo '</head><body>';
        if (!empty($str))
            echo $str . '<br />';
        echo '<a href="' . $url . '">如不能跳转,请点击此处</a>';
        echo '</body></html>';
        exit;
    }

    /**
     * 警告后刷新回到上一页
     */
    public static function refUrl($str='', $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        echo '<html>';
        $alert = !empty($str) ? 'alert("' . $str . '");' : '';
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        $preurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
        echo '<body>';
        self::playsound($sound);
        echo '<script lanage="javascript">' . $alert . 'window.location="' . $preurl . '"</script>';
        echo '</body>';
        echo '</html>';
        exit;
    }

    public static function refUrlEcho($str='', $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        $alert = !empty($str) ? 'alert("' . $str . '");' : '';
        echo '<html>';
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        $preurl = self::getPreUrl();
        echo '<meta http-equiv="refresh" content="3;url=' . $preurl . '">';
        echo '<body>';
        self::playsound($sound);
        if (!empty($str))
            echo $str . '<br />';
        echo '<a href="' . $preurl . '">如不能跳转,请点击此处</a>';
        echo '</body>';
        echo '</html>';
        exit;
    }

    /**
     * 输出警告并有两个选项
     *
     * @param unknown_type $str
     * @param unknown_type $yesUrl
     * @param unknown_type $noUrl
     */
    public static function alertYesNo($str, $yesUrl='', $noUrl='', $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        self::playsound($sound);
        echo '<script lanage="javascript">'
        . 'if(confirm("' . $str . '")){'
        . 'window.location="' . $yesUrl . '";'
        . '}else{'
        . 'window.location="' . $noUrl . '";'
        . '}</script>';
        exit;
    }

    public static function alertYesNoEcho($str, $yesUrl='', $noUrl='', $charset='utf-8', $sound=0) {
        header("Content-type:text/html;charset=$charset");
        echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
        self::playsound($sound);
//		echo '<script lanage="javascript">'
//			.'if(confirm("'.$str.'")){'
//			.'window.location="'.$yesUrl.'";'
//			.'}else{'
//			.'window.location="'.$noUrl.'";'
//			.'}</script>';
        echo '<body>';
        echo $str . '<br />';
        echo '<input type="button" value="确定" style="width:88px;height:31px;" onclick="window.location=\'' . $yesUrl . '\'" />&nbsp;&nbsp;';
        echo '<input type="button" value="取消" style="width:88px;height:31px;" onclick="window.location=\'' . $noUrl . '\'" />&nbsp;&nbsp;';
        exit;
    }

    /**
     * 播放声音
     * @param type $sound
     * @return type
     */
    private static function playsound($sound) {
        if (!$sound)
            return;
        if (!isset(wk::product()->scansound[$sound]))
            return;
        echo '<bgsound src="' . PICHOST . 'sound/' . wk::product()->scansound[$sound] . '">';
    }

    /**
     * 返回IP对应的地区名称
     *
     * @param unknown_type $ip
     * @param unknown_type $file
     * @return unknown
     */
    public static function getAddress($ip, $file = 'QQWry.Dat') {
        include_once dirname(__FILE__). '/ip.class.php';
        $QQWry = new ip(dirname(__FILE__). '/QQWry.Dat');
        $QQWry->QQWry($ip);
        //echo "您的IP是：".$ip."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;地理位置：".$QQWry->Country.'/'.$QQWry->Local;
        return $QQWry->Country; //.' '.$QQWry->Local;
    }

    /**
     * 获取当前用户真实IP
     *
     * @return string
     */
    public static function getIp() {
        if (isset($_SESSION['simulation_ip']))
            return $_SESSION['simulation_ip'];
        if (isset($_SERVER)) {
            if (isset($_SERVER ["HTTP_X_FORWARDED_FOR"])) {
                $realip = $_SERVER ["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER ["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER ["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER ["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
        if (isset($realip[16]))
            $realip = substr($realip, 0, 15);
        //不准,还要修改
        $trealip = explode('.', $realip);
        $realip = array();
        foreach($trealip as $value){
            $realip[] = intval($value);
        }
        return join('.', $realip);
    }

    /**
     * 将Unix时间转成Y-m-d
     *
     * @param int(10) $date
     * @return string
     */
    public static function formatDate($date='', $type = 1) {
        if (empty($date))
            $date = $_SERVER['REQUEST_TIME'];
        switch ($type) {
            case 1 :
                return date("Y-m-d", $date);
                break;
            case 2 :
                return date("m-d", $date);
                break;
            case 3 :
                return date("Y-m-d H:i", $date);
                break;
            case 4 :
                return date("Y-m-d H:i:s", $date);
                break;
            default:
                return $date;
                break;
        }
    }

    /**
     * 将Y-m-d换成unix时间
     */
    public static function dateYmdToUnix($date) {
        $newdate = explode('-', $date);
        if (empty($newdate))
            return $_SERVER['REQUEST_TIME'];
        $unixdate = mktime(0, 0, 0, $newdate[1], $newdate[2], $newdate[0]);
        return $unixdate;
    }

    /**
     * 自定义加密方式，防止被破解
     *
     * @param string $pass
     * @return string
     */
    public static function md99Pass($pass) {
        $salt = substr($pass, 0, 2);
        $pswd = md5(crypt($pass, $salt));
        return $pswd;
    }

    /**
     * 转义特殊字符
     *
     * @param  $string|array
     * @return string|array
     */
    public static function dhtmlspecialchars($string) {
        /*
          if (is_array ( $string )) {
          foreach ( $string as $key => $val ) {
          $string [$key] = self::dhtmlspecialchars ( $val ); //递归用法
          }
          } else {
          $string = preg_replace ( '/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1', $string  );
          }
          return $string;
         */
        //新方法
        $string = is_array($string) ? array_map('addslashes', $string) : addslashes($string);
        return $string;
    }

    /**
     * 反转解义字符
     *
     * @param unknown_type $string
     */
    public static function dhtmlstripslashes($string) {
        /*
          if(is_array( $string )) {
          foreach( $string as $key => $val ) {
          $string[$key] = self::dhtmlstripslashes($val);
          }
          } else {
          $string=stripslashes($string);
          }
          return $string;
         */
        //新方法
        $string = is_array($string) ? array_map('stripslashes', $string) : stripslashes($string);
        return $string;
    }

    

    /**
     * 输出水印代码
     *
     * @param unknown_type $name
     * @param unknown_type $path
     * @return unknown
     */
    public static function water($name, $other='') {
        $path = "/watercode.php?a=aboc";
        //return '<input name="watercode" type="text" class="watercode" id="watercode_'.$name.'" style="width:40px;" size="4" maxlength="4" /><img onclick="this.src=\''.$path.'&t='. $name .'&id=\'+Math.random()*5;dm_water_'.$name.'.alt=\'看不清楚?点击换一个\';" style="cursor:pointer;" id="dm_water_'.$name.'" name="dm_water_'.$name.'" alt="点击更换验证码" align="absmiddle" src="'.$path.'&t='.$name.'" />';
        return '<input name="watercode" ' . $other . ' type="text" id="watercode_' . $name . '" size="4" maxlength="4" onfocus="if(this.value==\'\'&&dm_water_' . $name . '.src==\'' . STATICURL . 'images/getcode.gif\')dm_water_' . $name . '.src=\'' . $path . '&t=' . $name . '\';dm_water_' . $name . '.alt=\'看不清楚?点击换一个\';" /><img onclick="this.src=\'' . $path . '&t=' . $name . '&id=\'+Math.random()*5;dm_water_' . $name . '.alt=\'看不清楚?点击换一个\';" style="cursor:pointer;" id="dm_water_' . $name . '" name="dm_water_' . $name . '" alt="点击获取验证码" align="absmiddle" src="' . STATICURL . 'images/getcode.gif" />';
    }

    /*
     * 判断验证码
     * $input 输入值
     * $codeName 验证码的变量
     * return bool
     */

    public static function checkWater($input, $codeName='watercode') {
        //print_r($_SESSION);
        //echo $input.'-'.$_SESSION[$codeName];
        if (@(md5(strtolower($input)) != md5(strtolower($_SESSION[$codeName]))) || empty($input)) {
            $_SESSION[$codeName] = md5(rand(1000, 4000)); //重置
            return false;
        } else {
            $_SESSION[$codeName] = md5(rand(1000, 4000)); //重置
            return true;
        }
    }

    /**
     * 将秒数化成天时分秒
     * 
     */
    public static function showTime($time) {
        $time = abs($time);
        $t['d'] = $time >= 86400 ? floor($time / 86400) : 0;
        $t['h'] = ($timeH = $time - $t['d'] * 86400) >= 0 ? floor($timeH / 3600) : floor($time / 3600);
        $t['m'] = ($timeM = $time - $t['d'] * 86400 - $t['h'] * 3600) >= 0 ? floor($timeM / 60) : floor($time / 60);
        $t['s'] = ($timeS = $time - $t['d'] * 86400 - $t['h'] * 3600 - $t['m'] * 60) >= 0 ? floor($timeS) : $time;
        return $t;
    }

    /**
     * 由discuz获取关键词
     */
    public static function getKeyword($subject='', $message='', $charset='gbk') {
//    	$subject = $_GET['subjectenc'];
//		$message = $_GET['messageenc'];
        //$subjectenc = strip_tags($subject);
        //$messageenc = strip_tags($message);
        //discuz!关键词不可用，现在改用sohu博客的
        $subjectenc = rawurlencode(strip_tags($subject));
        $messageenc = rawurlencode(strip_tags(preg_replace("/\[.+?\]/U", '', $message)));
        $data = @implode('', file("http://keyword.discuz.com/related_kw.html?ics=$charset&ocs=$charset&title=$subjectenc&content=$messageenc"));
        if ($data) {

            $parser = xml_parser_create();
            xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
            xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
            xml_parse_into_struct($parser, $data, $values, $index);
            xml_parser_free($parser);

            $kws = array();

            foreach ($values as $valuearray) {
                if ($valuearray['tag'] == 'kw' || $valuearray['tag'] == 'ekw') {
                    $valuearray['value'] = iconv('utf-8', $charset, $valuearray['value']);
                    $kws[] = trim($valuearray['value']);
                }
            }

            /*
              $return = '';
              if($kws) {
              foreach($kws as $kw) {
              $kw = htmlspecialchars($kw);
              $return .= $kw.' ';
              }
              $return = htmlspecialchars($return);
              }
             */

            return $kws;
        }
        /*
          $url = "http://q.sohu.com/service/tag/extr.html?text=".urlencode($subjectenc.$messageenc).'&_=';
          $content = @file_get_contents($url);
          $content = str_replace('tags:','',$content);
          $rows = explode(' ',$content);
          return $rows;
         */
        //	return array();           
    }

    /**
     * 编码转换
     */
    public static function myIconv($str, $inchar='utf-8', $outchar='gbk') {
        if (is_array($str)) {
            $newstr = array();
            foreach ($str as $key => $row) {
                $newstr[$key] = self::myIconv($row, $inchar, $outchar);
            }
        } else {
            $newstr = iconv($inchar, $outchar, $str);
        }
        return $newstr;
    }

    /**
     * 获取当前时间
     */
    public static function getNowTime() {
        return $_SERVER['REQUEST_TIME'];
    }

   

    /**
     * 获取当前URL地址
     *
     * @return unknown
     */
    public static function getThisUrl() {
        return "http://" . $_SERVER ['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }

    /**
     * 获取上一个URL地址
     * 
     */
    public static function getPreUrl() {
        return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/';
    }

    /**
     * escape加密
     *
     * @param unknown_type $str
     * @return unknown
     */
    public static function escape($str) {
        preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/", $str, $r);
        $ar = $r[0];
        foreach ($ar as $k => $v) {
            if (ord($v[0]) < 128) {
                $ar[$k] = rawurlencode($v);
            } else {
                $ar[$k] = "%u" . bin2hex(iconv("utf-8", "UCS-2", $v));
            }
        }
        return join("", $ar);
    }

    /**
     * escape解密
     *
     * @param unknown_type $str
     * @return unknown
     */
    public static function unescape($str) {
        $str = rawurldecode($str);
        preg_match_all("/(?:%u.{4})|.+/", $str, $r);
        $ar = $r[0];
        foreach ($ar as $k => $v) {
            if (substr($v, 0, 2) == "%u" && strlen($v) == 6) {
                $ar[$k] = iconv("UCS-2", "utf-8", pack("H4", substr($v, -4)));
            }
        }
        return join("", $ar);
    }

    /**
     * 将内容修改为js适合调用的格式
     *
     * @param unknown_type $str
     * @return unknown
     */
    public static function jsCode($str) {
        $str = str_replace(array('"', "'"), array('\"', "\'"), $str);
        $str = explode("\r\n", $str);
        if (!is_array($str))
            return;
        $a = '';
        foreach ($str as $row) {
            if (!empty($row))
                $a .= "document.writeln('$row');\r\n";
        }
        return $a;
    }

    /**
     * 自定义转义字符
     *
     * @param unknown_type $str
     */
    public static function myAddslashes($str) {
        //$str = is_array($str)?array_map('addslashes',$str):addslashes($str);
        if (is_array($str)) {
            foreach ($str as $key => $value) {
                $str[$key] = self::myAddslashes($value);
            }
        } else {
            $str = addslashes($str);
            //$str = str_replace(array('"',"'"),array("&quot;","&#039;"),$str);
        }
        return $str;
    }

    /**
     * 自定义反转义字符
     *
     * @param unknown_type $str
     * @return unknown
     */
    public static function myStripslashes($str) {
        //$str = is_array($str)?array_map('stripslashes',$str):stripslashes($str);
        if (is_array($str)) {
            foreach ($str as $key => $value) {
                $str[$key] = self::myAddslashes($value);
            }
        } else {
            $str = stripslashes($str);
        }
        return $str;
    }

    /**
     * 自动打打魔术引号,针对post,get,cookie,request
     *
     */
    public static function openMagic_quotes_gpc() {
        if (!get_magic_quotes_gpc()) {
            //$_POST = array_map('addslashes',$_POST);
            if (isset($_POST))
                $_POST = self::myAddslashes($_POST);
            if (isset($_GET))
                $_GET = self::myAddslashes($_GET);
            if (isset($_COOKIE))
                $_COOKIE = self::myAddslashes($_COOKIE);
            //if (isset($_REQUEST))$_REQUEST = self::myAddslashes($_REQUEST);
        }
    }

    /**
     * 设置上一个url地址到cookie中
     *
     * @return unknown
     */
    public static function setPreUrlToCookie($path='/', $domain='') {
        return self::dmSetCookie(self::getPreUrl(), 'url', self::getNowTime() + 3600, $path, $domain);
    }

    /**
     * 获取cookie中的上一个url地址
     *
     * @return unknown
     */
    public static function getPreUrlFromCookie() {
        if (!isset($_COOKIE['url']) || empty($_COOKIE['url']))
            return '';
        return urldecode($_COOKIE['url']);
    }

    /**
     * 格式化时间为45秒前，30分钟前，一天前，二天前，三天前
     * @param unknown_type $times
     * @param $small 三天前的时候精确到  d/h:i:s
     */
    public static function formatDatetime($time, $format = "Y-m-d H:i:s") {
        $nowtime = self::getNowTime();
        $difftime = $nowtime - $time;
//    echo $difftime.'///';
        if ($difftime >= 0) {
            if ($difftime <= 3) {
                return '刚刚';
            }
            if ($difftime < 60) {
                return $difftime . '秒前';
            }
            if ($difftime < 1800) {
                $difftime = ceil($difftime / 60);
                return $difftime . "分钟前";
            }
            if ($difftime < 2700) {
                return "半小时前";
            }
            if ($difftime < 36000) {
                $difftime = ceil($difftime / 3600);
                return $difftime . "小时前";
            }
            $today0 = strtotime(date("Y-m-d"));
            if ($time > $today0) {
                return "今天" . date("H:i");
            }
            if (date("Ymd", $time) == date("Ymd", strtotime("-1 day"))) {
                return "昨天" . date("H:i");
            }
            if (date("Ymd", $time) == date("Ymd", strtotime("-2 day"))) {
                return "前天" . date("H:i");
            }
        }
        return date($format, $time);
    }
    
    static function object_to_array($obj){
	$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
	foreach ($_arr as $key => $val){
		$val = (is_array($val) || is_object($val)) ? self::object_to_array($val) : $val;
		$_arr[$key] = $val;
	}
	return $_arr;
    }

}

?>